package Classes.GameRecords// ActionScript file
{
	import Classes.GameDatas.BuildType;
	import Classes.GameDatas.GD_Building_Func;
	import Classes.GameDatas.GD_Player;
	import Classes.GameUtils.GD_deal;
	import Classes.GameUtils.GU_MsgDeal;
	import Classes.GameViews.GV_Map;

	/**
	 *建筑记录类
	 * 记录建筑时的一些列操作 
	 * @author 葛增文
	 * 
	 */	
	public class GR_Build extends GameRecord
	{
		private static var _instance:GR_Build;
		private var _changeBuildings:Object;
		public function GR_Build():void
		{
			this.init();
		}
		
		private function init():void
		{
			this._changeBuildings=new Object;
		}
		
		/**
		 *增加建筑 
		 * @param buildingData
		 * 
		 */		
		public function addBuilding(buildingData:GD_Building_Func):void
		{
			if(this.changeBuildings.hasOwnProperty(buildingData.serverID))throw new Error("建筑已存在");
			this.changeBuildings[buildingData.serverID]=buildingData;
		}
		
		public function deleteBuilding(buildingData:GD_Building_Func):void
		{
			delete this.changeBuildings[buildingData.serverID];
		}
		
		/**
		 *改变结束，发送消息 
		 * 
		 */		
		public function sendBuildRecord(callBack:Function=null):void
		{
			for each(var changeBuilding:GD_Building_Func in this.changeBuildings)
			{
				switch(changeBuilding.buildType.changeType)
				{
					case BuildType.BUY:
						this.addMsg(12100,{itemType:changeBuilding.shopId,pil:1});
					case BuildType.PLACE:
						this.addMsg(12301,{tmpID:changeBuilding.tmpID,buildingType:changeBuilding.buildingItemType,coordinate:changeBuilding.coordinate});
						break;
					case BuildType.MOVE:
						this.addMsg(12300,{buildingID:changeBuilding.serverID,coordinate:changeBuilding.coordinate});
						break;
					case BuildType.DELETE:
						this.addMsg(12302,{buildingID:changeBuilding.serverID});
						break;
					case BuildType.INVALID:
						continue;
						break;
				}
			}
			this.send(callBack);
		}
		
		/**
		 *恢复建筑的操作 
		 * 
		 */		
		override protected function recoverRecord():void
		{
			for each(var changeBuilding:GD_Building_Func in this.changeBuildings)
			{
				changeBuilding.recoverChange();
			}
		}
		
		/**
		 *替换临时建筑ID 
		 * @param msgObj
		 * 
		 */		
		override protected function finishRecord(msgObj:Object):void
		{
			if(msgObj.replaceAry)
			{
				for each(var replaceObj:Object in msgObj.replaceAry)
				{
					GV_Map.instance.mapData.replaceBuildingID(replaceObj.tmpID,replaceObj.buildingID);
				}
			}
			
			if(int(msgObj.msgType)==22600)
			{
				if(GD_deal.instance.getStr(msgObj.itemType)==401 || GD_deal.instance.getStr(msgObj.itemType)==402)
				{
					GD_Player.instance.changeBuildMiss(msgObj.value,msgObj.itemType);
				}else{
					GU_MsgDeal.instance.dealMsg_22600(msgObj);
				}
			}else{
				(GU_MsgDeal.instance["dealMsg_"+msgObj.msgType] as Function)(msgObj);
			}
		}
		
		override public function reset():void
		{
			for each(var changeBuilding:GD_Building_Func in this.changeBuildings)
			{
				changeBuilding.cleanBuildType();
			}
			this._changeBuildings=new Object;
			super.reset();
		}

		/*getter & setter*/
		public static function get instance():GR_Build
		{
			if(!_instance)_instance=new GR_Build;
			return _instance;
		}

		public function get changeBuildings():Object
		{
			return _changeBuildings;
		}


	}
}